/*
Copyright [2020] [https://www.stylefeng.cn]

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

  http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

Guns采用APACHE LICENSE 2.0开源协议，您在使用过程中，需要注意以下几点：

1.请不要删除和修改根目录下的LICENSE文件。
2.请不要删除和修改Guns源码头部的版权声明。
3.请保留源码和相关描述文件的项目出处，作者声明等。
4.分发源码时候，请注明软件出处 https://gitee.com/stylefeng/guns-separation
5.在修改包名，模块名称，项目代码等时，请注明软件出处 https://gitee.com/stylefeng/guns-separation
6.若您的项目无法满足以上几点，可申请商业授权，获取Guns商业授权许可，请在官网购买授权，地址为 https://www.stylefeng.cn
 */
package cn.stylefeng.qz.action.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import cn.hutool.core.io.FileUtil;
import cn.stylefeng.qz.action.service.ContentService;
import cn.stylefeng.qz.business.bean.CategoryBean;
import cn.stylefeng.qz.business.bean.ContentBean;
import cn.stylefeng.qz.business.enums.CategoryTypeEnum;
import cn.stylefeng.qz.business.model.Category;
import cn.stylefeng.qz.action.service.CategoryService;
import cn.stylefeng.qz.core.pojo.response.ResponseData;
import cn.stylefeng.qz.core.util.CmsParserUtil;
import lombok.extern.slf4j.Slf4j;
import net.mingsoft.basic.entity.AppEntity;
import net.mingsoft.basic.util.BasicUtil;
import net.mingsoft.mdiy.util.ParserUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * 一个示例接口
 *
 * @author stylefeng
 * @date 2020/4/9 18:09
 */
@RestController
@RequestMapping("/genernate")
@Slf4j
public class GeneraterActionController {


    @Autowired
    private CategoryService categoryService;


    @Autowired
    private ContentService contentService;


    /**
     * 生成列表的静态页面
     *
     * @param request
     * @param response
     * @param categoryId
     */
    @RequestMapping("/{categoryId}/genernateColumn")
    @ResponseBody
    public ResponseEntity genernateColumn(HttpServletRequest request, HttpServletResponse response, @PathVariable String categoryId) throws IOException {
        // 获取站点id
        AppEntity app = BasicUtil.getApp();

        //栏目列表
        List<Category> columns = new ArrayList<Category>();

        if ("0".equals(categoryId)) {// 0更新所有栏目

            columns = categoryService.queryAll();
        } else { //选择栏目更新
            Category category = new Category();
            category.setId(categoryId);
            columns = categoryService.queryChilds(category);
        }
        //文章列表
        List<CategoryBean> articleIdList = null;

        // 获取栏目列表模版
        for (Category column : columns) {

            ContentBean contentBean = new ContentBean();
            contentBean.setCategoryId(column.getId());

            articleIdList = contentService.queryIdsByCategoryIdForParser(contentBean);
            // 判断列表类型
            switch (CategoryTypeEnum.get(column.getCategoryType())) {
                //TODO 暂时先用字符串代替
                case LIST: // 列表

                    // 判断模板文件是否存在
                    if (!FileUtil.exist(ParserUtil.buildTempletPath(column.getCategoryListUrl()))) {
                        log.error("模板不存在：{}", column.getCategoryUrl());
                        continue;
                    }

                    CmsParserUtil.generateList(column, articleIdList.size(),"html");
                    break;
                case COVER:// 单页
                    if (articleIdList.size() == 0) {
                        CategoryBean columnArticleIdBean = new CategoryBean();
                        CopyOptions copyOptions = CopyOptions.create();
                        copyOptions.setIgnoreError(true);
                        BeanUtil.copyProperties(column, columnArticleIdBean, copyOptions);
                        articleIdList.add(columnArticleIdBean);
                    }
                    CmsParserUtil.generateBasic(articleIdList,"html");
                    break;
            }
        }

        return ResponseEntity.ok().build();

    }

}
